<?php

/**
 * Yii extension implementing jquery iealert
 * See : http://nmsdvid.com/iealert/
 * 
 * @package extensions.iealert
 * @author hightman
 */
class IEAlertWidget extends CInputWidget
{
	/**
	 * @var string which version of Internet Explorer will the modal box appear.
	 * It accepts 4 parameters: ie6|ie7|ie8|ie9, default to ie8
	 */
	public $support;

	/**
	 * @var string the title text
	 * Default: "Did you know that your Internet Explorer is out of date ?"
	 */
	public $title;

	/**
	 * @var string the text what is under the title text
	 * It accepts HTML tags like the HTML font tag
	 */
	public $text;

	/**
	 * @var string the title text inside the upgrade button, default to "Upgrade" 
	 */
	public $upgradeTitle;

	/**
	 * @var string the URL where the user will be sent when he clicks the Upgrade button
	 * Default: http://browsehappy.com/
	 */
	public $upgradeLink;

	/**
	 * @var boolean whether you can close the modal box by clicking the overlay, default to false
	 */
	public $overlayClose;

	/**
	 * @var boolean whether you can close the modal box by clicking a button, default to true
	 */
	public $closeBtn;

	public function run()
	{
		// reigster clientScript
		$assetUrl = Yii::app()->assetManager->publish(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'theplugin');
		$cs = Yii::app()->clientScript; /* @var $cs CClientScript */
		$cs->registerCssFile($assetUrl . '/iealert/style.css');
		$cs->registerCoreScript('jquery');
		$cs->registerScriptFile($assetUrl . '/iealert' . (YII_DEBUG ? '' : '.min') . '.js');
		// gen options
		$options = array();
		$_options = array('support', 'title', 'text', 'upgradeTitle', 'upgradeLink', 'overlayClose', 'closeBtn');
		foreach ($_options as $name)
		{
			if ($this->$name !== null)
				$options[$name] = $this->$name;
		}
		// gen script
		$script = 'jQuery(\'body\').iealert(' . (count($options) > 0 ? CJSON::encode($options) : '') . ');';
		$cs->registerScript(__CLASS__, $script, CClientScript::POS_READY);
	}
}